<?php

/**
 * sfFormGui actions.
 *
 * @package    plugintest
 * @subpackage sfFormGui
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class sfFormGuiActions extends sfActions
{
  public function executeIndex($request)
  {
    $this->sf_form_guiList = sfFormGuiPeer::getAllByRank();
    $this->criteria = new Criteria;
    $this->criteria->addAscendingOrderByColumn(sfFormGuiQuestionPeer::RANK);
    
    $this->getContext()->getResponse()->addJavascript('/sfFormGuiPlugin/js/sfFormGui.class.js','last');
  }

  public function executeCreate()
  {
    $this->form = new sfFormGuiForm();

    $this->setTemplate('edit');
  }

  public function executeEdit($request)
  {
    $this->form = new sfFormGuiForm(sfFormGuiPeer::retrieveByPk($request->getParameter('id')));
  }

  public function executeUpdate($request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new sfFormGuiForm(sfFormGuiPeer::retrieveByPk($request->getParameter('id')));

    $this->form->bind($request->getParameter('sf_form_gui'));
    if ($this->form->isValid())
    {
      $sf_form_gui = $this->form->save();

      $this->redirect('sfFormGui/edit?id='.$sf_form_gui->getId());
    }

    $this->setTemplate('edit');
  }

  public function executeDelete($request)
  {
    $this->forward404Unless($sf_form_gui = sfFormGuiPeer::retrieveByPk($request->getParameter('id')));

    $sf_form_gui->delete();

    $this->redirect('sfFormGui/index');
  }
  
  public function executeSort($request)
  {
      $order = $request->getParameter('order-'.$this->getModuleName());
      $this->forward404Unless(is_array($order));
      $flag = sfFormGuiPeer::doSort($order);
      return $this->renderText($this->getContext()->getI18N()->__($flag ? 'Status updated' : 'Error' ));
  }
  
  public function executeShow($request) {
      $this->session_id = session_id();
      $this->sf_form_gui = sfFormGuiPeer::retrieveByPk($request->getParameter('id'));
      $this->forward404Unless($this->sf_form_gui);
      $this->page = $request->getParameter('page', 1);
      $c = new Criteria;
      $c->addAscendingOrderByColumn(sfFormGuiQuestionPeer::RANK);
      $this->form = $this->sf_form_gui->getFormObject($c);
      if($request->isMethod('post')) {
          $this->form->bind($request->getParameter('sf_form_gui_questions'), $request->getFiles('sf_form_gui_questions'));
          if($this->form->isValid()) {
              $this->sf_form_gui->setResponses($this->session_id, $this->form);
              $this->page++;
              //$this->redirect('sfFormGui/show?id='.$request->getParameter('id').'&page='.$this->page);
          }
      }
  }
  
  public function executeStat($request) {
      $this->session_id = session_id();
      $this->sf_form_gui = sfFormGuiPeer::retrieveByPk($request->getParameter('id'));
      $this->forward404Unless($this->sf_form_gui);
      
  }
}
